package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.YtCustomerMsg;
import com.ruoyi.system.service.IYtCustomerInvoiceService;
import com.ruoyi.system.service.IYtCustomerMsgService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 客户基本信息Controller
 *
 * @author magichaohao
 * @date 2021-05-10
 */
@Controller
@RequestMapping("/system/customer")
public class YtCustomerMsgController extends BaseController
{
    private String prefix = "system/customer";

    @Autowired
    private IYtCustomerMsgService ytCustomerMsgService;

    @Autowired
    private IYtCustomerInvoiceService ytCustomerInvoiceService;

    @RequiresPermissions("system:customer:view")
    @GetMapping()
    public String customer()
    {
        return prefix + "/customer";
    }

    /**
     * 查询客户基本信息列表
     */
    @RequiresPermissions("system:customer:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(YtCustomerMsg ytCustomerMsg)
    {
        startPage();
        List<YtCustomerMsg> list = ytCustomerMsgService.selectYtCustomerMsgList(ytCustomerMsg);
        return getDataTable(list);
    }

    /**
     * 导出客户基本信息列表
     */
    @RequiresPermissions("system:customer:export")
    @Log(title = "客户基本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(YtCustomerMsg ytCustomerMsg)
    {
        List<YtCustomerMsg> list = ytCustomerMsgService.selectYtCustomerMsgList(ytCustomerMsg);
        ExcelUtil<YtCustomerMsg> util = new ExcelUtil<YtCustomerMsg>(YtCustomerMsg.class);
        return util.exportExcel(list, "客户基本信息数据");
    }

    /**发票*/
    @GetMapping("/detail/{customerId}")
    public String detailUrl(@PathVariable("customerId") Long customerId, ModelMap mmap)
    {
        mmap.put("customerId", customerId);
        return prefix + "/detail";
    }
//   /system/customer/contract/{customerId}

    /**合同*/
    @GetMapping("/contract/{customerId}")
    public String contractUrl(@PathVariable("customerId") Long customerId, ModelMap mmap)
    {
        mmap.put("customerId", customerId);
        return prefix + "/contract";
    }

//    http://localhost/system/management/management/3
//    /system/customer/management/{customerId}
    @GetMapping("/management/{customerId}")
    public String managementUrl(@PathVariable("customerId") Long customerId, ModelMap mmap)
    {
        mmap.put("customerId", customerId);
        return prefix + "/management";
    }



    /**
     * 新增客户基本信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存客户基本信息
     */
    @RequiresPermissions("system:customer:add")
    @Log(title = "客户基本信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(YtCustomerMsg ytCustomerMsg)
    {
        return toAjax(ytCustomerMsgService.insertYtCustomerMsg(ytCustomerMsg));
    }

    /**
     * 修改客户基本信息
     */
    @GetMapping("/edit/{customerId}")
    public String edit(@PathVariable("customerId") Long customerId, ModelMap mmap)
    {
        YtCustomerMsg ytCustomerMsg = ytCustomerMsgService.selectYtCustomerMsgById(customerId);
        mmap.put("ytCustomerMsg", ytCustomerMsg);
        return prefix + "/edit";
    }

    /**
     * 修改保存客户基本信息
     */
    @RequiresPermissions("system:customer:edit")
    @Log(title = "客户基本信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(YtCustomerMsg ytCustomerMsg)
    {
        return toAjax(ytCustomerMsgService.updateYtCustomerMsg(ytCustomerMsg));
    }

    /**
     * 删除客户基本信息
     */
    @RequiresPermissions("system:customer:remove")
    @Log(title = "客户基本信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(ytCustomerMsgService.deleteYtCustomerMsgByIds(ids));
    }
}
